package com.zlll.winner.controller.lht;

import com.alibaba.fastjson.JSONObject;
import com.zlll.winner.business.model.lhtUser.LhtUser;
import com.zlll.winner.business.service.lhtUser.ILhtUserIdentityTypeService;
import com.zlll.winner.business.service.lhtUser.ILhtUserService;
import com.zlll.winner.common.BaseController;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/lhtUser")
public class LhtUserController  extends BaseController {

    @Reference
    private ILhtUserIdentityTypeService typeService;
    @Reference
    private ILhtUserService userService;

    /**
     * 获取所以的类型
     * @return
     */
    @GetMapping("identityType")
    public Object getIdentityType(){
        return typeService.getIdentityTypeList();
    }

    /**
     * 用户列表
     *{
     * 	"pageSize":10,  //显示分页条数
     * 	"pageNo":1,     //页码
     * 	"phone":"15913181397",  //手机号
     * 	"nickname":"张",    //昵称
     * 	"identityId":2     //身份ID
     * }
     * @param data
     * @return
     */
    @PostMapping("getLhtUserList")
    public Object getLhtUserList(@RequestBody JSONObject data){
        Integer pageSize = data.getInteger("pageSize") == null ? 20 :data.getInteger("pageSize");
        Integer pageNo = data.getInteger("pageNo") == null ? 1 :data.getInteger("pageNo");
        String phone = data.getString("phone");
        String nickname = data.getString("nickname");
        Long identityId = data.getLong("identityId");
        return userService.getLhtUser(phone,nickname,identityId,pageNo,pageSize);
    }

    /**
     * 修改用户身份
     * {
     *
     * 	"id":6,    //用户ID
     * 	"identityIds":",2,3,4,"   //身份ID ,分割
     * }
     * @param data
     * @return
     */
    @PostMapping("update")
    public Object update(@RequestBody JSONObject data){
        Long id = data.getLong("id");
        String identityIds = data.getString("identityIds");
        return userService.updateLhtUserIdentity(id,identityIds);
    }

    /**
     * 修改用户身份
     * {
     *
     * 	"id":6,    //用户ID
     * 	"identityIds":",2,3,4,"   //身份ID ,分割
     * }
     * @param data
     * @return
     */
    @PostMapping("save")
    public Object save(@RequestBody JSONObject data){
        LhtUser user = data.toJavaObject(LhtUser.class);
        Long[] identityIds = data.getJSONArray("identityIds").toJavaObject(Long[].class);
        return userService.addUser(user, identityIds, getUser().getName());
    }

}
